import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../../../constant/color_constant.dart';

class CustomTextFieldWidget extends StatefulWidget {
  final String inputValue;
  final String hint;
  final List<TextInputFormatter>? textInputFormatterList;
  final ValueChanged<String> updateMessageCallback2;
  TextInputType keyboardType = TextInputType.text;
  bool obscureText = true;

  CustomTextFieldWidget(
      {required this.hint,
      required this.inputValue,
      required this.updateMessageCallback2,
      required this.textInputFormatterList,
      required this.keyboardType,
      required this.obscureText});

  @override
  _CustomTextFieldWidgetState createState() => _CustomTextFieldWidgetState(
        hint: hint,
        inputValue: inputValue,
        updateMessageCallback: updateMessageCallback2,
        textInputFormatterList: textInputFormatterList,
        keyboardType: keyboardType,
        obscureText: obscureText,
      );
}

class _CustomTextFieldWidgetState extends State<CustomTextFieldWidget> {
  final TextEditingController _controller = TextEditingController();
  final String inputValue;
  final String hint;
  final List<TextInputFormatter>? textInputFormatterList;
  TextInputType keyboardType = TextInputType.text;
  bool obscureText = false;

  ValueChanged<String> updateMessageCallback;
  _CustomTextFieldWidgetState(
      {required this.hint,
      required this.inputValue,
      required this.updateMessageCallback,
      required this.textInputFormatterList,
      required this.keyboardType,
      required this.obscureText});
  @override
  Widget build(BuildContext context) {
    return TextField(
        keyboardType: keyboardType,
        obscureText: obscureText,
        inputFormatters: textInputFormatterList,
        decoration: InputDecoration(
            isDense: true,
            border: InputBorder.none, // 移除默认边框
            focusedBorder: const UnderlineInputBorder(
              // 获取焦点时的下划线
              borderSide: BorderSide(
                color: ColorConstant.CommonColorWhite, // 修改下划线颜色
                width: 0, // 修改下划线宽度
              ),
            ),
            enabledBorder: const UnderlineInputBorder(
              // 默认状态下的下划线
              borderSide: BorderSide(
                color: ColorConstant.CommonColorWhite, // 修改下划线颜色
                width: .0, // 修改下划线宽度
              ),
            ),
            hintText: hint,
            hintStyle: TextStyle(
                fontFamily: "PingFag SC",
                fontWeight: FontWeight.w500,
                fontSize: 18.sp,
                color: ColorConstant.CommonColorGray)),
        textAlign: TextAlign.start,
        style: TextStyle(
            fontFamily: "PingFag SC",
            fontWeight: FontWeight.w500,
            fontSize: 18.sp,
            color: ColorConstant.CommonColorBlack),
        onEditingComplete: () {
          // 当用户点击完成按钮时，这个函数会被调用;
          // 你可以在这里添加你的逻辑，比如验证输入或提交数据
        },
        onSubmitted: (value) {
          FocusScope.of(context).unfocus();
        },
        onChanged: (value) {
          // 这个回调函数会在文本变化时不断被调用
          print('文本变化: $value');
          updateMessageCallback(value);
        });
  }

  @override
  void didUpdateWidget(CustomTextFieldWidget oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (oldWidget.obscureText != widget.obscureText) {
      setState(() {
        obscureText = !obscureText;
      });
    }
  }

  @override
  void dispose() {
    // 清理控制器，避免内存泄漏
    _controller.dispose();
    super.dispose();
  }
}
